const util = require('../../../utils/util')
const api = require('../../../config/api')
var user = require('../../../utils/user')
var app = getApp()
// pages/user/index/index.js

Page({
    data: {
        userInfo: {
            nickName: '点击登录',
            avatarUrl: 'http://yanxuan.nosdn.127.net/8945ae63d940cc42406c3f67019c5cb6.png'
        },
        hasLogin: false
    },
    onShow() {
        // 获取用户登录信息
        user.checkLogin().then(res => {
            app.globalData.hasLogin = true
            let userInfo = wx.getStorageSync('userInfo');
            this.setData({
                userInfo: userInfo,
                hasLogin: true
            })
        }).catch(() => {
            app.globalData.hasLogin = false
        })

    },
    goLogin() {
        if(!this.data.hasLogin){
            wx.navigateTo({
                url: '/pages/auth/login/login',
            });
        }
    },
    outLogin(){
        wx.showModal({
            title: '退出登录',
            confirmlColor:'#b4282d',
            content: '确认退出登录吗？',
            success (res) {
              if (res.confirm) {
                util.request(api.AuthLogout,{},'POST').then(res=>{
                    // console.log(res);
                    // 退出登录成功
                })
                wx.removeStorageSync('token')
                wx.removeStorageSync('userInfo')
                //  wx.reLaunch关闭了内存中所有保留的页面，再跳转到目标页面
                wx.reLaunch({
                  url: '/pages/index/index',
                })

              } else if (res.cancel) {
                  return
              }
            }
          })
    }
})